package com.wcfsy.wechat.wx.um.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wcfsy.wechat.wx.um.entity.User;
import com.wcfsy.wechat.wx.um.service.IUserConService;
import com.wcfsy.wechat.wx.util.JsonMessage;

@Controller
@RequestMapping("/cms/operator")
public class OperatorController extends AbstractXssDefenderController {
	private static Logger logger = LoggerFactory
			.getLogger(OperatorController.class);
	@Resource
	private IUserConService userConService;

	@RequestMapping({ "/view/page" })
	public ModelAndView pre_find_opertator() {
		return new ModelAndView("operator/manage_operator");
	}

	@RequestMapping({ "/find" })
	@ResponseBody
	public Object find(User param, int page, int rows,
			HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("limit", "pagination");
		if ((null != param.getRealName())
				&& (!"".equalsIgnoreCase(param.getUserName()))) {
			paramMap.put("userName", param.getUserName());
		}
		if ((null != param.getRealName())
				&& (!"".equalsIgnoreCase(param.getRealName()))) {
			paramMap.put("realName", param.getRealName());
		}
		if ((null != param.getStatus())
				&& (!"".equalsIgnoreCase(param.getStatus()))) {
			paramMap.put("status", param.getStatus());
		}
		return this.userConService.findByParam(paramMap, page, rows);
	}

	@RequestMapping({ "/modify/page" })
	public ModelAndView preModifyOperator(int id) {
		int userId = id;
		User user = this.userConService.getById(userId);
		ModelAndView mav = new ModelAndView("operator/modify_operator");
		mav.addObject("id", Integer.valueOf(userId));
		mav.addObject("userName", user.getUserName());
		mav.addObject("password", user.getPassword());
		mav.addObject("realName", user.getRealName());
		mav.addObject("telephone", user.getTelephone());
		mav.addObject("email", user.getEmail());
		mav.addObject("remark", user.getRemark());
		return mav;
	}

	@RequestMapping({ "/modify" })
	@ResponseBody
	public JsonMessage modify(User user) {
		JsonMessage jsonMessage = new JsonMessage();
		boolean validator = false;
		try {
			/*
			 * User loginUser = (User) SecurityManager
			 * .getSessionAttribute("SESSION_USER");
			 */
			/*
			 * user.setDepartment(org.getOrgCode());
			 * user.setOrgName(org.getOrgName());
			 */
			user.setUpdateId(Integer.valueOf(1));
			user.setUpdateDate(new Date());
			this.userConService.update(user);
			jsonMessage.setSuccess("succeed");
			jsonMessage.setMessage("用户修改成功");

		} catch (Exception e) {
			logger.error("Error,when add user in UserController!", e);
			jsonMessage.setSuccess("error");
			jsonMessage.setMessage("用户修改失败");
		}

		return jsonMessage;
	}

	@RequestMapping({ "/del/{id}" })
	@ResponseBody
	public JsonMessage del(@PathVariable("id") int userId) {
		JsonMessage jsonMessage = new JsonMessage();
		String userName = this.userConService.getById(userId).getUserName();
		try {
			this.userConService.deleteById(userId);
			jsonMessage.setSuccess("succeed");
			jsonMessage.setMessage("用户删除成功");
		} catch (Exception e) {
			logger.error("Error,when delete User in UserController", e);
			jsonMessage.setSuccess("error");
			jsonMessage.setMessage("用户删除失败");
		}
		return jsonMessage;
	}

	@RequestMapping({ "/reset_pwd/{userName}" })
	@ResponseBody
	public JsonMessage modifyPwd(String userName) {
		JsonMessage jsonMessage = new JsonMessage();
		try {
			this.userConService.resetPwd(userName);
			jsonMessage.setSuccess("succeed");
			jsonMessage.setMessage("密码重置成功");
		} catch (Exception e) {
			logger.error("Error,when reset password in UserController", e);
			jsonMessage.setSuccess("error");
			jsonMessage.setMessage("密码重置失败");
		}
		return jsonMessage;
	}

	@RequestMapping({ "/add/page" })
	public ModelAndView preAddOpertator() {
		ModelAndView mav = new ModelAndView("operator/add_operator");
		return mav;
	}

	@RequestMapping({ "/username/check" })
	@ResponseBody
	public JsonMessage checkUserName(String userName) {
		JsonMessage jsonMessage = new JsonMessage();
		User user = this.userConService.getByUserName(userName);
		if (null != user) {
			jsonMessage.setSuccess("error");
			jsonMessage.setMessage("该用户已存在");
		} else {
			jsonMessage.setSuccess("succeed");
			jsonMessage.setMessage("恭喜，该用户名可以使用");
		}
		return jsonMessage;
	}

	@RequestMapping({ "/add" })
	@ResponseBody
	public JsonMessage add(User user, HttpServletRequest request) {
		JsonMessage jsonMessage = new JsonMessage();
		try {
			user.setCreateId(Integer.valueOf(Integer.parseInt(String.valueOf(1))));
			this.userConService.add(user);
			jsonMessage.setSuccess("succeed");
			jsonMessage.setMessage("添加用户成功！");
		} catch (Exception e) {
			logger.error("Error,when add user in UserController!", e);
			jsonMessage.setSuccess("error");
			jsonMessage.setMessage("用户添加失败！");
			return jsonMessage;
		}
		return jsonMessage;
	}
}
